﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;

namespace DALL.View
{
    public class T_PUB_CUSTOMERINFO
    {
        public T_PUB_CUSTOMERINFO()
        { }

        /// <summary>
        /// 智能提示客户姓名
        /// </summary>
        /// <returns></returns>
        public DataSet GetCustomerName()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select DISTINCT GROOM_NAME,BRIDE_NAME FROM T_PUB_CUSTOMERINFO ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 智能提示客户新郎姓名
        /// </summary>
        /// <returns></returns>
        public DataSet GetGroomName()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select DISTINCT GROOM_NAME FROM T_PUB_CUSTOMERINFO ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 智能提示客户新娘姓名
        /// </summary>
        /// <returns></returns>
        public DataSet GetBrideName()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select DISTINCT BRIDE_NAME FROM T_PUB_CUSTOMERINFO ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        #region  Method

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Model.View.T_PUB_CUSTOMERINFO model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_PUB_CUSTOMERINFO(");
            strSql.Append("CUSTOMER_ID,GROOM_NAME,BRIDE_NAME,GROOMPOST,BRIDEPOST,GROOMPHONE,BRIDEPHONE,GROOMQQ,BRIDEQQ,GROOMAGE,BRIDEAGE,HOUSEADDRESS,WEDDING,BRIDEADDRESS,MARRIAGECAR,[TIME],HOTEL,TABLENUM,[RANK],ELDER,YOUNGER)");

            strSql.Append(" values (");
            strSql.Append("@CUSTOMER_ID,@GROOM_NAME,@BRIDE_NAME,@GROOMPOST,@BRIDEPOST,@GROOMPHONE,@BRIDEPHONE,@GROOMQQ,@BRIDEQQ,@GROOMAGE,@BRIDEAGE,@HOUSEADDRESS,@WEDDING,@BRIDEADDRESS,@MARRIAGECAR,@TIME,@HOTEL,@TABLENUM,@RANK,@ELDER,@YOUNGER)");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "CUSTOMER_ID", DbType.String, model.CUSTOMER_ID);
            db.AddInParameter(dbCommand, "GROOM_NAME", DbType.String, model.GROOM_NAME);
            db.AddInParameter(dbCommand, "BRIDE_NAME", DbType.String, model.BRIDE_NAME);
            db.AddInParameter(dbCommand, "GROOMPOST", DbType.String, model.GROOMPOST);
            db.AddInParameter(dbCommand, "BRIDEPOST", DbType.String, model.BRIDEPOST);
            db.AddInParameter(dbCommand, "GROOMPHONE", DbType.String, model.GROOMPHONE);
            db.AddInParameter(dbCommand, "BRIDEPHONE", DbType.String, model.BRIDEPHONE);
            db.AddInParameter(dbCommand, "GROOMQQ", DbType.String, model.GROOMQQ);
            db.AddInParameter(dbCommand, "BRIDEQQ", DbType.String, model.BRIDEQQ);
            db.AddInParameter(dbCommand, "GROOMAGE", DbType.Int32, model.GROOMAGE);
            db.AddInParameter(dbCommand, "BRIDEAGE", DbType.Int32, model.BRIDEAGE);
            db.AddInParameter(dbCommand, "HOUSEADDRESS", DbType.String, model.HOUSEADDRESS);
            db.AddInParameter(dbCommand, "WEDDING", DbType.String, model.WEDDING);
            db.AddInParameter(dbCommand, "BRIDEADDRESS", DbType.String, model.BRIDEADDRESS);
            db.AddInParameter(dbCommand, "MARRIAGECAR", DbType.String, model.MARRIAGECAR);
            db.AddInParameter(dbCommand, "TIME", DbType.String, model.TIME);
            db.AddInParameter(dbCommand, "HOTEL", DbType.String, model.HOTEL);
            db.AddInParameter(dbCommand, "TABLENUM", DbType.Int32, model.TABLENUM);
            db.AddInParameter(dbCommand, "RANK", DbType.Int32, model.RANK);
            db.AddInParameter(dbCommand, "ELDER", DbType.Int32, model.ELDER);
            db.AddInParameter(dbCommand, "YOUNGER", DbType.Int32, model.YOUNGER);
            int rows = db.ExecuteNonQuery(dbCommand);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.View.T_PUB_CUSTOMERINFO model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_PUB_CUSTOMERINFO set ");
            strSql.Append("GROOM_NAME=@GROOM_NAME,");
            strSql.Append("BRIDE_NAME=@BRIDE_NAME,");
            strSql.Append("GROOMPOST=@GROOMPOST,");
            strSql.Append("BRIDEPOST=@BRIDEPOST,");
            strSql.Append("GROOMPHONE=@GROOMPHONE,");
            strSql.Append("BRIDEPHONE=@BRIDEPHONE,");
            strSql.Append("GROOMQQ=@GROOMQQ,");
            strSql.Append("BRIDEQQ=@BRIDEQQ,");
            strSql.Append("GROOMAGE=@GROOMAGE,");
            strSql.Append("BRIDEAGE=@BRIDEAGE,");
            strSql.Append("HOUSEADDRESS=@HOUSEADDRESS,");
            strSql.Append("WEDDING=@WEDDING,");
            strSql.Append("BRIDEADDRESS=@BRIDEADDRESS,");
            strSql.Append("MARRIAGECAR=@MARRIAGECAR,");
            strSql.Append("TIME=@TIME,");
            strSql.Append("HOTEL=@HOTEL,");
            strSql.Append("TABLENUM=@TABLENUM,");
            strSql.Append("RANK=@RANK,");
            strSql.Append("ELDER=@ELDER,");
            strSql.Append("YOUNGER=@YOUNGER");
            strSql.Append(" where CUSTOMER_ID=@CUSTOMER_ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "CUSTOMER_ID", DbType.String, model.CUSTOMER_ID);
            db.AddInParameter(dbCommand, "GROOM_NAME", DbType.String, model.GROOM_NAME);
            db.AddInParameter(dbCommand, "BRIDE_NAME", DbType.String, model.BRIDE_NAME);
            db.AddInParameter(dbCommand, "GROOMPOST", DbType.String, model.GROOMPOST);
            db.AddInParameter(dbCommand, "BRIDEPOST", DbType.String, model.BRIDEPOST);
            db.AddInParameter(dbCommand, "GROOMPHONE", DbType.String, model.GROOMPHONE);
            db.AddInParameter(dbCommand, "BRIDEPHONE", DbType.String, model.BRIDEPHONE);
            db.AddInParameter(dbCommand, "GROOMQQ", DbType.String, model.GROOMQQ);
            db.AddInParameter(dbCommand, "BRIDEQQ", DbType.String, model.BRIDEQQ);
            db.AddInParameter(dbCommand, "GROOMAGE", DbType.Int32, model.GROOMAGE);
            db.AddInParameter(dbCommand, "BRIDEAGE", DbType.Int32, model.BRIDEAGE);
            db.AddInParameter(dbCommand, "HOUSEADDRESS", DbType.String, model.HOUSEADDRESS);
            db.AddInParameter(dbCommand, "WEDDING", DbType.String, model.WEDDING);
            db.AddInParameter(dbCommand, "BRIDEADDRESS", DbType.String, model.BRIDEADDRESS);
            db.AddInParameter(dbCommand, "MARRIAGECAR", DbType.String, model.MARRIAGECAR);
            db.AddInParameter(dbCommand, "TIME", DbType.String, model.TIME);
            db.AddInParameter(dbCommand, "HOTEL", DbType.String, model.HOTEL);
            db.AddInParameter(dbCommand, "TABLENUM", DbType.Int32, model.TABLENUM);
            db.AddInParameter(dbCommand, "RANK", DbType.Int32, model.RANK);
            db.AddInParameter(dbCommand, "ELDER", DbType.Int32, model.ELDER);
            db.AddInParameter(dbCommand, "YOUNGER", DbType.Int32, model.YOUNGER);
            int rows = db.ExecuteNonQuery(dbCommand);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string CUSTOMER_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_PUB_CUSTOMERINFO ");
            strSql.Append(" where CUSTOMER_ID=@CUSTOMER_ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "CUSTOMER_ID", DbType.String, CUSTOMER_ID);
            int rows = db.ExecuteNonQuery(dbCommand);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.View.T_PUB_CUSTOMERINFO GetModel(string CUSTOMER_ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select CUSTOMER_ID,GROOM_NAME,BRIDE_NAME,GROOMPOST,BRIDEPOST,GROOMPHONE,BRIDEPHONE,GROOMQQ,BRIDEQQ,GROOMAGE,BRIDEAGE,HOUSEADDRESS,WEDDING,BRIDEADDRESS,MARRIAGECAR,TIME,HOTEL,TABLENUM,RANK,ELDER,YOUNGER from T_PUB_CUSTOMERINFO ");
            strSql.Append(" where CUSTOMER_ID=@CUSTOMER_ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "CUSTOMER_ID", DbType.String, CUSTOMER_ID);
            Model.View.T_PUB_CUSTOMERINFO model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select CUSTOMER_ID,GROOM_NAME,BRIDE_NAME,GROOMPOST,BRIDEPOST,GROOMPHONE,BRIDEPHONE,GROOMQQ,BRIDEQQ,GROOMAGE,BRIDEAGE,HOUSEADDRESS,WEDDING,BRIDEADDRESS,MARRIAGECAR,TIME,HOTEL,TABLENUM,RANK,ELDER,YOUNGER ");
            strSql.Append(" FROM T_PUB_CUSTOMERINFO ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public List<Model.View.T_PUB_CUSTOMERINFO> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select CUSTOMER_ID,GROOM_NAME,BRIDE_NAME,GROOMPOST,BRIDEPOST,GROOMPHONE,BRIDEPHONE,GROOMQQ,BRIDEQQ,GROOMAGE,BRIDEAGE,HOUSEADDRESS,WEDDING,BRIDEADDRESS,MARRIAGECAR,TIME,HOTEL,TABLENUM,RANK,ELDER,YOUNGER ");
            strSql.Append(" FROM T_PUB_CUSTOMERINFO ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Model.View.T_PUB_CUSTOMERINFO> list = new List<Model.View.T_PUB_CUSTOMERINFO>();
            Database db = DatabaseFactory.CreateDatabase();
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public Model.View.T_PUB_CUSTOMERINFO ReaderBind(IDataReader dataReader)
        {
            Model.View.T_PUB_CUSTOMERINFO model = new Model.View.T_PUB_CUSTOMERINFO();
            object ojb;
            model.CUSTOMER_ID = dataReader["CUSTOMER_ID"].ToString();
            model.GROOM_NAME = dataReader["GROOM_NAME"].ToString();
            model.BRIDE_NAME = dataReader["BRIDE_NAME"].ToString();
            model.GROOMPOST = dataReader["GROOMPOST"].ToString();
            model.BRIDEPOST = dataReader["BRIDEPOST"].ToString();
            model.GROOMPHONE = dataReader["GROOMPHONE"].ToString();
            model.BRIDEPHONE = dataReader["BRIDEPHONE"].ToString();
            model.GROOMQQ = dataReader["GROOMQQ"].ToString();
            model.BRIDEQQ = dataReader["BRIDEQQ"].ToString();
            ojb = dataReader["GROOMAGE"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.GROOMAGE = (int)ojb;
            }
            ojb = dataReader["BRIDEAGE"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.BRIDEAGE = (int)ojb;
            }
            model.HOUSEADDRESS = dataReader["HOUSEADDRESS"].ToString();
            model.WEDDING = dataReader["WEDDING"].ToString();
            model.BRIDEADDRESS = dataReader["BRIDEADDRESS"].ToString();
            model.MARRIAGECAR = dataReader["MARRIAGECAR"].ToString();
            ojb = dataReader["TIME"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.TIME = (DateTime)ojb;
            }
            model.HOTEL = dataReader["HOTEL"].ToString();
            ojb = dataReader["TABLENUM"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.TABLENUM = (int)ojb;
            }
            ojb = dataReader["RANK"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.RANK = (int)ojb;
            }
            ojb = dataReader["ELDER"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ELDER = (int)ojb;
            }
            ojb = dataReader["YOUNGER"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.YOUNGER = (int)ojb;
            }
            return model;
        }

        #endregion  Method
    }
}
